package com.baidu.erp.visitor;

import com.baidu.erp.composite.PermissionNode;
import com.baidu.erp.dao.PermissionDao;
import com.baidu.erp.domain.Permission;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
public class DeletePermissionVisitor implements PermissionVisitor {

    private PermissionDao permissionDao;
    private SqlSession sqlSession;

    public DeletePermissionVisitor() {
    }

    public DeletePermissionVisitor(PermissionDao permissionDao, SqlSession sqlSession) {
        this.permissionDao = permissionDao;
        this.sqlSession = sqlSession;
    }

    @Override
    public void visit(PermissionNode permissionNode) {
        List<Permission> permissionList = permissionDao.getSonPermissionById(permissionNode.getId());
        List<PermissionNode> permissionNodeList = new ArrayList<>(permissionList.size());
        permissionList.forEach(p -> {
            permissionNodeList.add(p.clone(PermissionNode.class));
        });

        if (permissionNodeList != null && permissionList.size() > 0) {
            permissionNodeList.forEach(node->node.accept(this));
        }

        permissionDao.del(permissionNode.getId());
        sqlSession.commit();

        //System.out.println(permissionNode.getName()+"被删除");
    }
}
